package top.xierongsheng.code;

import lombok.Getter;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 定义返回类型格式
 *
 * @author 谢荣生
 * @date 2020-04-14 10:37
 */
public class R extends HashMap<String, Object>{

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 测试
     *
     * @param args
     * @auther 谢荣生
     * @datetime 2020/8/20 16:44
     */
    public static void main(String[] args) {
        R r = R.ok().zip("code", 222)
                .zip(new HashMap<String, Object>() {{
                    put("张三", "我问问");
                }})
                .zip(new String[]{}, null);
        System.out.println(r);
    }

    //*********************************************************************** 构造器
    public R() {
        super();
    }

    public R(Integer code, String message) {
        zip("code", code);
        zip("message", message);
    }

    //*********************************************************************** zip
    public R zip(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R zip(Map<String, Object> map) {
        super.putAll(map);
        return this;
    }

    public R zip(String[] keys, Object[] values) {
        Map<String, Object> map = new HashMap<>();
        if (keys != null && values != null) {
            int size = Math.min(keys.length, values.length);
            for (int i = 0; i < size; i++) {
                map.put(keys[i], values[i]);
            }
        }
        return zip(map);
    }

    //*********************************************************************** 创建
    public static R create(Integer code, String message) {
        return new R(code, message);
    }

    //*********************************************************************** 失败
    public static R fail() {
        return create(State.fail.code, State.fail.message);
    }

    public static R fail(String msg) {
        return create(State.fail.code, msg);
    }

    public static R fail(Integer code, String msg) {
        return create(code, msg);
    }

    public static R fail(Map<String, Object> map) {
        return fail().zip(map);
    }

    //*********************************************************************** 错误
    public static R error() {
        return create(State.error.code, State.error.message);
    }

    public static R error(String msg) {
        return create(State.error.code, msg);
    }

    public static R error(Integer code, String msg) {
        return create(code, msg);
    }

    public static R error(Map<String, Object> map) {
        return error().zip(map);
    }

    //*********************************************************************** 成功
    public static R ok() {
        return create(State.ok.code, State.ok.message);
    }

    public static R ok(String msg) {
        return create(State.ok.code, msg);
    }

    public static R ok(Integer code, String msg) {
        return create(code, msg);
    }

    public static R ok(Map<String, Object> map) {
        return ok().zip(map);
    }

    //*********************************************************************** 枚举
    public static R state(State eState) {
        return create(eState.code, eState.message);
    }

    //*********************************************************************** 状态

    /**
     * 状态枚举
     */
    @Getter
    public enum State {
        ok(0, "成功"), fail(1, "失败"), error(500, "服务异常,请销后重试"),
        token_expire(100, "token已过期");

        State(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        private final Integer code;
        private final String message;

    }


    /**
     * 登录超时
     *
     * @return
     */
    public static R timeout() {
        return create(99, "登录超时，请重新登录");
    }

    /**
     * 权限错误
     *
     * @return
     */
    public static R powererror() {
        return create(98, "您没有此权限");
    }

    /**
     * 权限错误
     *
     * @param msg
     * @return
     */
    public static R powererror(Object msg) {
        if (msg != null && msg.toString().length() != 0) {
            return create(98, msg.toString());
        } else {
            return create(98, "您没有此权限");
        }
    }

}
